package com.itita.ww2.game.user;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.itita.ww2.model.user.UserProfile;
import com.itita.ww2.model.user.battle.BaseBattleState;
import com.itita.ww2.model.user.battle.BaseBattleState.BattleType;
import com.itita.ww2.model.user.battle.MissionBattleState;
import com.itita.ww2.model.user.battle.WorldBattleState;
import com.itita.ww2.model.worldmap.Base;
import com.smartfoxserver.v2.entities.data.ISFSObject;

/**
 * 玩家战斗管理器
 * @author NorthLan
 *
 */
public class UserBattleManager {
	private static final Logger logger = LoggerFactory.getLogger(UserBattleManager.class);
	// 
	private UserProfile userProfile;

	private BaseBattleState battleState; // 战斗状态

	public UserBattleManager(UserProfile userProfile) {
		this.userProfile = userProfile;
	}

	/**
	 * 进入战斗
	 */
	public void enterBattle(BattleType type, ISFSObject army, Object... data) {
		endBattle(true, false); // 结束前一场战斗
		switch (type) {
		case Mission:
			battleState = new MissionBattleState(userProfile, army, (int) data[0], (int) data[1], (String) data[2]);
			break;
		case WorldBattle:
			battleState = new WorldBattleState(userProfile, army, (Base) data[0]);
			break;
		case AGGRESSION:
			logger.warn("Aggression Battle is not available!");
			break;
		case AlBattle:
			logger.warn("AlBattle is not available!");
			break;
		default:
			break;
		}
		if (battleState != null) {
			battleState.startBattle();
		}
	}

	/**
	 * 结束战斗
	 */
	public void endBattle(boolean online, boolean victory) {
		if (battleState != null) {
			battleState.endBattle(online, victory);
			battleState = null;
		}
	}

	@SuppressWarnings("unchecked")
	public <T> T getBattleState(BattleType type) {
		if (battleState == null) {
			return null;
		}
		if (battleState.getBattleType() == type) {
			return (T) battleState;
		}
		return null;
	}

	//	/**
	//	 * 添加战报,最大数量为5条
	//	 */
	//	public synchronized void addAggrBattleLog(ISFSObject data, boolean offensive) {
	//		final int maxCount = 5;
	//		Table t = UserService.getInstance().getCacheAggrUserDataTable();
	//		int uid = userProfile.getUid();
	//		if (offensive) { // 进攻战报
	//			ISFSArray offensiveObj = DataUtil.arrValueOf(t.getValue(RedisUtil.genericKey(uid), "offensive_report"));
	//			if (offensiveObj.size() >= maxCount) {
	//				long temp = Long.MIN_VALUE;
	//				int index = Integer.MIN_VALUE;
	//				for (int i = 0; i < offensiveObj.size(); ++i) {
	//					ISFSObject log = offensiveObj.getSFSObject(i);
	//					long startTime = log.getLong("startTime");
	//					if (i == 0) { // 首次
	//						temp = startTime;
	//						index = i;
	//						continue;
	//					}
	//					if (startTime < temp) {
	//						temp = startTime;
	//						index = i;
	//					}
	//				}
	//				offensiveObj.removeElementAt(index); // 删之
	//			}
	//			offensiveObj.addSFSObject(data);
	//			// 保个存
	//			t.setValue(RedisUtil.genericKey(uid), "offensive_report", offensiveObj.toJson());
	//		} else { // 防守战报
	//			ISFSArray defenseObj = DataUtil.arrValueOf(t.getValue(RedisUtil.genericKey(uid), "defense_report"));
	//			if (defenseObj.size() >= maxCount) {
	//				long temp = Long.MIN_VALUE;
	//				int index = Integer.MIN_VALUE;
	//				for (int i = 0; i < defenseObj.size(); ++i) {
	//					ISFSObject log = defenseObj.getSFSObject(i);
	//					long startTime = log.getLong("startTime");
	//					if (i == 0) { // 首次
	//						temp = startTime;
	//						index = i;
	//						continue;
	//					}
	//					if (startTime < temp) {
	//						temp = startTime;
	//						index = i;
	//					}
	//				}
	//				defenseObj.removeElementAt(index); // 删之
	//			}
	//			defenseObj.addSFSObject(data);
	//			// 保个存
	//			t.setValue(RedisUtil.genericKey(uid), "defense_report", defenseObj.toJson());
	//		}
	//	}

	//	/**
	//	 * 获取离线战报
	//	 */
	//	public synchronized ISFSObject getAggrBattleLog() {
	//		Table t = UserService.getInstance().getCacheAggrUserDataTable();
	//		ISFSArray offensiveObj = DataUtil
	//				.arrValueOf(t.getValue(RedisUtil.genericKey(this.userProfile.getUid()), "offensive_report"));
	//		ISFSArray defenseObj = DataUtil
	//				.arrValueOf(t.getValue(RedisUtil.genericKey(this.userProfile.getUid()), "defense_report"));
	//		ISFSObject ret = SFSObject.newInstance();
	//		ret.putSFSArray("MyLog", offensiveObj);
	//		ret.putSFSArray("Log", defenseObj);
	//		return ret;
	//	}

	//////////////////////////////////////////////.Event

	public void onDisconnectEvent() {
		endBattle(false, false);
	}
}
